package cn.fl.ass.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cmm.user.facade.system.condition.SysUsrFunAuthRQueryCondition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysUsrApiService;
import cmm.user.facade.system.condition.SysUsrQueryCondition;
import cmm.user.facade.system.dto.SysUsrDTO;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.ass.service.IAssDealSellService;
import cn.fl.ass.transform.AssDealSellLossVOTransform;
import cn.fl.ass.transform.AssDealSellVOTransform;
import cn.fl.ass.vo.AssDealSellLossVO;
import cn.fl.ass.vo.AssDealSellVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import fl.ass.facade.api.IAssDealSellApi;
import fl.ass.facade.condition.AssDealSellLossQueryCondition;
import fl.ass.facade.condition.AssDealSellQueryCondition;
import fl.ass.facade.constant.InterfaceConstants;
import fl.ass.facade.dto.AssDealSellDTO;
import fl.ass.facade.dto.AssDealSellLossDTO;
import fl.ass.facade.dto.MessageDTO;

/**
 * @Description: 处置-变卖（事业部及优车） 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class AssDealSellServiceImpl implements IAssDealSellService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IAssDealSellService.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private IAssDealSellApi assDealSellApi;
	/**
	 * 数据字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;
	/**
	 * 系统用户 API 暴露服务
	 */
	@Autowired
	private ISysUsrApiService sysUsrApiService;

	@Override
	public AssDealSellVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			return null;
		}
		//查询处置主表信息
		MessageDTO<AssDealSellDTO> response = this.assDealSellApi.findAssDealSellById(id);
		AssDealSellDTO data = response.getData();
		if (data != null && data.getDealSellMgrId() != null) {
			// 优车销售经理
			try {
				SysUsrDTO sysUsrDTO = sysUsrApiService.selectSysUsrById(data.getDealSellMgrId(), CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
				if (sysUsrDTO != null) {
					data.setDealSellMgrUsrNm(sysUsrDTO.getUsrNm());
					data.setDealSellMgrRlNm(sysUsrDTO.getRefRlNm());
				}
			} catch (BaseException e) {
				BaseLogUtils.newLogger(String.format("[dealSellId=%s]查询优车销售经理[%s]用户信息出错", data.getId(), data.getDealSellMgrId())).setThrowable(e).info();
			}
		}
		return AssDealSellVOTransform.toVO(data);
	}

	@Override
	public Long add(AssDealSellVO entity) throws BaseException{
		if(null == entity){
			return null;
		}
		MessageDTO<String> response = this.assDealSellApi.saveAssDealSell(AssDealSellVOTransform.toDTO(entity));
		return response.getId();
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			return false;
		}
		//return this.assDealSellApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(AssDealSellVO entity) throws BaseException{
		if(null == entity){
			return false;
		}
		//返回逻辑值-JL-20170407
		MessageDTO<String> response = this.assDealSellApi.updateAssDealSell(AssDealSellVOTransform.toDTO(entity));
		return response.isSuccess();
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		//return this.assDealSellApi..searchListPage(page);
		//TODO 业务逻辑
		return null;
	}
	@Override
	public List<AssDealSellVO> searchAssDealSellList(
			AssDealSellQueryCondition condition) throws BaseException {
		MessageDTO<AssDealSellDTO> dtoList = this.assDealSellApi.searchList(condition);
		List<AssDealSellVO> voList = new ArrayList<AssDealSellVO>();
		if(null != dtoList.getDatas()){
			for (AssDealSellDTO dto : dtoList.getDatas()) {
				voList.add(AssDealSellVOTransform.toVO(dto));
			}
		}
		return voList;
		}
	 /**
     * 分页查询所有变卖亏损信息
     * @param page
     * @return
     */
	@Override
	public Pager AssDealSellLossByPage(Pager page) throws BaseException {
		if(null == page)page = new Pager();
		Pager assDealSellLossByPage = this.assDealSellApi.AssDealSellLossByPage(page);
		return assDealSellLossByPage;
	}
	 /**
     * 不分页查询
	 * @param SellId 
     * @param AssDealSellQueryCondition
     * @return
     */
	@Override
	public List<AssDealSellLossVO> selectLossList(
			Long  SellId) throws BaseException {
		MessageDTO<AssDealSellLossDTO> dtoList = this.assDealSellApi.searchLossList(SellId);
		//亏损原因
		String sysCd = CurrentThreadContext.getCurrentSysCd(); 
		String orgCd = CurrentThreadContext.getCurrentOrgCd();
        Map<String, String> lossCauseCdMap = this.sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, InterfaceConstants.LOSS_CAUSE_CD);
        List<AssDealSellLossVO> voList = new ArrayList<AssDealSellLossVO>();
		if(null != dtoList.getDatas()){
			for (AssDealSellLossDTO dto : dtoList.getDatas()) {
				if(lossCauseCdMap.containsKey(dto.getLossCauseCd())){
					dto.setLossCauseNm(lossCauseCdMap.get(dto.getLossCauseCd()));
				}
				Long crtUsrId = dto.getCrtUsrId();
				if(null != crtUsrId){
					 SysUsrDTO selectSysUsrById = this.sysUsrApiService.selectSysUsrById(crtUsrId, sysCd, orgCd);
						dto.setCrtUsrName(selectSysUsrById.getRefRlNm());
				}
				voList.add(AssDealSellLossVOTransform.toVO(dto));
			}
		}
		return voList;
	}
	/*
	 * 新增亏损信息
	 */
	@Override
	public Long addassDealSellLoss(AssDealSellLossVO entity) {
		if(null == entity){
			return null;
		}
		/*if(StringUtils.isNotBlank(entity.getLossCauseCd()) && entity.getLossCauseCd().equals("006")  && StringUtils.isBlank(entity.getCostRem())){//选择其他原因  并且备注为空
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "请添加备注信息!");
			
		}*/
		AssDealSellLossDTO assDealSellLossDTO = AssDealSellLossVOTransform.toDTO(entity);
		MessageDTO<String> response = this.assDealSellApi.addassDealSellLoss(assDealSellLossDTO);
		return response.getId();
	}
	/*
	 * 修改亏损信息
	 */
	@Override
	public Long modifyassDealSellLoss(AssDealSellLossVO entity) {
		if(null == entity){
			return null;
		}
		/*if( StringUtils.isNotBlank(entity.getLossCauseCd())  && entity.getLossCauseCd().equals("006") && StringUtils.isBlank(entity.getCostRem())){//选择其他原因  并且备注为空
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "请添加备注信息!");
			
		}*/
		MessageDTO<String> response = this.assDealSellApi.modifyassDealSellLoss(AssDealSellLossVOTransform.toDTO(entity));
		return response.getId();
	}

//	@Override
//	public void checkLawsuitSts(Long sueApplyId) throws BaseException {
//		assDealSellApi.checkLawsuitSts(sueApplyId);
//	}
//
//	/**
//	 * 查询"优车处置（销售经理）"角色下的用户
//	 * @param condition
//	 * @return
//	 * @throws BaseException
//	 */
//	@Override
//	public List<AssDealSellDTO> searchSellManagerList(SysUsrFunAuthRQueryCondition condition) throws BaseException {
//		return assDealSellApi.searchSellManagerList(condition);
//	}
}
